<template>
  <div class="login-container">
    <video
      src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4"
      autoplay
      loop="loop"
      muted="muted"
    ></video>

    <div class="center_top">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <img
            src="https://slwl-admin.itheima.net/static/img/logo1.33ef7fa0.png"
            alt=""
          />
        </div>

        <!-- 账号 -->
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="账号"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <!-- 验证码 -->
        <el-form-item prop="yzm">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            placeholder="验证码"
            type="text"
            tabindex="1"
            auto-complete="on"
            v-model="loginForm.yzm"
          />
          <img :src="Verification" alt="" class="yzm" @click="yzmgx" />
        </el-form-item>

        <!-- 登录 -->
        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click="Login"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { verificationApi } from '@/api/user'
export default {
  name: 'Login',

  data() {
    return {
      key: new Date().getTime(), // 接口需要的key
      Verification: '', // 验证码
      loginForm: {
        // username: 'demo', // 用户名
        // password: '888itcast.CN764%...', // 密码
        username: 'shenlingadmin', // 用户名
        password: '123456', // 密码
        yzm: '' // 验证码
      },
      rules: {
        username: [
          {
            required: true, // 必填项
            trigger: 'blur', // 校验触发的时机
            message: '请输入手机号' // 未通过校验的提示信息,
          },
          {
            min: 6,
            max: 16,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 16,
            message: '密码长度应该为6-16位之间',
            trigger: 'blur'
          }
        ],
        yzm: [
          {
            required: true,
            message: '请输入验证码',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 6,
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {},
  // 获取验证码图片
  async created() {
    try {
      const { request } = await verificationApi(this.key)
      this.Verification = request.responseURL
      // this.key = config.params.key
      // console.log(this.key)
    } catch (error) {
      console.log(error)
    }
  },
  methods: {
    // 显示密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 点击登录
    async Login() {
      const data = {
        account: this.loginForm.username, // 账号
        code: this.loginForm.yzm, // 验证码
        key: this.key, // 与获取验证码时的key保持一致
        password: this.loginForm.password // 密码
      }
      await this.$store.dispatch('user/login', data)
      this.$router.push('/')
    },
    // 验证码更新
    async yzmgx() {
      this.key = new Date().getTime()
      try {
        const { request } = await verificationApi(this.key)
        this.Verification = request.responseURL
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;

    .el-input__inner {
      color: black;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #b1b1b1;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 420px;
    max-width: 100%;
    padding: 30px 35px 0;
    margin: 160px auto;
    overflow: hidden;
    border-radius: 20px;
    background-color: white;
  }
  // 按钮
  .el-button {
    font-size: 18px;
    background-color: #e15536;
    margin-top: 10px;
    padding: 20px 0;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    text-align: center;
    img {
      margin-bottom: 30px;
      width: 50%;
      height: 50%;
    }

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

video {
  width: 100%;
  height: 100%;
  object-fit: fill;
}
.center_top {
  position: absolute;
  top: 0%;
  left: 35%;
}
// 验证码
.yzm {
  position: absolute;
  height: 100%;
  right: 0;
}
</style>
